package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.CaseInfo;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.SupervisionInfo;
import com.ldj.hibernate.SupervisionInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.SupervisionInfoForm;

public class SupervisionInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String userId = SystemSession.getSupUserId(request);
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		String supvInfoCode = request.getParameter("supvInfoCode");
		String empInfoName = request.getParameter("empInfoName");
		List list = dao.findAllPage(userId, supvInfoCode, empInfoName);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		SupervisionInfo info = dao.findById(request.getParameter("sid"));
		request.setAttribute("sInfo", info);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		SupervisionInfo info = dao.findById(request.getParameter("sid"));
		request.setAttribute("sInfo", info);
//		String ii = request.getParameter("page");
//		System.out.println(ii);
		request.setAttribute("nowPage",request.getParameter("page"));
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoForm sForm = (SupervisionInfoForm)form;
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		SupervisionInfo info = dao.findById(request.getParameter("sid"));
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(sForm.getSupvInfoUnitId());
		info.setEmployerInfo(eInfo);
//		info.setSupvInfoCode(sForm.getSupvInfoCode());
		info.setSupvInfoDate(sForm.getDateSupvInfoDate());
		info.setSupvInfoEmergency(sForm.getSupvInfoEmergency());
		info.setSupvInfoType(sForm.getSupvInfoType());
		if("2".equals(sForm.getSupvInfoType())){
			info.setSupvInfoPensonNum(sForm.getSupvInfoPensonNum());
			info.setSupvInfoMoney(sForm.getSupvInfoMoney());
		}else if("3".equals(sForm.getSupvInfoType())){
			info.setSupvInfoPensonNum(sForm.getSupvInfoPensonNum());
			info.setSupvInfoMoney("");
		}else{
			info.setSupvInfoPensonNum("");
			info.setSupvInfoMoney("");
		}
		info.setSupvInfoDetail(sForm.getSupvInfoDetail());
		info.setSupvInfoRemark(sForm.getSupvInfoRemark());
		dao.merge(info);
		
		//日志
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SupervisionInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", eInfo.getEmpInfoName()));
		logDao.save(s);
        //修改后返回修改之前的页面
		return new ActionForward("/SupervisionInfo.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoForm sForm = (SupervisionInfoForm)form;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	    String year = df.format(new Date());
	    SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
	    String supvInfoCode = "001";
	    int code = dao.findMaxCode(year) + 1;
	    if(code < 10)
	    	supvInfoCode = "00" + code;
	    else if(code < 100)
	    	supvInfoCode = "0" + code;
	    supvInfoCode = year + supvInfoCode;
		SupervisionInfo info = new SupervisionInfo();
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(sForm.getSupvInfoUnitId());
		info.setEmployerInfo(eInfo);
		info.setSupvInfoCode(supvInfoCode);
		info.setSupvInfoDate(sForm.getDateSupvInfoDate());
		info.setSupvInfoEmergency(sForm.getSupvInfoEmergency());
		info.setSupvInfoType(sForm.getSupvInfoType());
		if("2".equals(sForm.getSupvInfoType())){
			info.setSupvInfoPensonNum(sForm.getSupvInfoPensonNum());
			info.setSupvInfoMoney(sForm.getSupvInfoMoney());
		}else if("3".equals(sForm.getSupvInfoType())){
			info.setSupvInfoPensonNum(sForm.getSupvInfoPensonNum());
		}
		info.setSupvInfoDetail(sForm.getSupvInfoDetail());
		info.setSupvInfoRemark(sForm.getSupvInfoRemark());
		info.setSupvInfoOrganId(SystemSession.getSupUserInfo(request).getPerFileOrganCode());	
		info.setSupvInfoIsCase("0");
		info.setSupvInfoDelOper("0");
		info.setSupvInfoInspectorId(SystemSession.getSupUserId(request));
		
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SupervisionInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", eInfo.getEmpInfoName()));
		logDao.save(s);
		return new ActionForward("/SupervisionInfo.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		List list = dao.findFromIdArray(request.getParameterValues("sid"));
		String  supvInfoCode="";
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				SupervisionInfo info = (SupervisionInfo)list.get(i);
				supvInfoCode+=info.getSupvInfoCode()+ ",";
				info.setSupvInfoDelOper("1");
				dao.merge(info);
			}
		}
		if(supvInfoCode.length() > 0)
			supvInfoCode = supvInfoCode.substring(0, supvInfoCode.length() - 1);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SupervisionInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", supvInfoCode));
		logDao.save(s);
		return new ActionForward("/SupervisionInfo.do?method=list", true);
	}
	
	public ActionForward toCase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SupervisionInfoDAO dao = new SupervisionInfoDAO(request, response);
		SupervisionInfo info = dao.findById(request.getParameter("sid"));
		CaseInfo cInfo = new CaseInfo();
		cInfo.setEmployerInfo(info.getEmployerInfo());
		cInfo.setCaseInfoBasiccondiBeginres(info.getSupvInfoDetail());
		cInfo.setCaseInfoRemark(info.getSupvInfoRemark());
		cInfo.setCaseInfoSource("005001");
		request.setAttribute("cInfo", cInfo);
		info.setSupvInfoIsCase("1");
		dao.merge(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("11");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SupervisionInfo.case").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getSupvInfoCode()));
		logDao.save(s);
		return mapping.findForward("toCase");
	}
}